package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.transport.DriverBindRecordEntity;
import com.sgb.kjwl.model.response.ListResponseV1;
import com.sgb.net.util.RequestUtil;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.dialog.DialogHelper;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class DriverBindRecordViewModel extends BaseViewModel {

    public MutableLiveData<ListResponseV1<List<DriverBindRecordEntity>>> ldList = new MutableLiveData<>();
    private int mPage = 1;

    public DriverBindRecordViewModel(@NonNull @NotNull Application application) {
        super(application);
    }

    /**
     * 获取绑定记录
     */
    public void getRecordList(Context context, String carNo, boolean isRefresh) {
        if (isRefresh) {
            mPage = 1;
        }
        RequestUtil requestUtil = RequestUtil.map();
        requestUtil.add("carNo", carNo);
        requestUtil.add("limit", 15);
        requestUtil.add("page", mPage);
        DialogHelper.showProgressDialog(context, "加载中");
        NewAPIManager.getInstance().getLogisticsAPI().getDriverBindRecord(requestUtil.buildMap())
                .enqueue(new ICallBack<BaseEntity<ListResponseV1<List<DriverBindRecordEntity>>>>() {
                    @Override
                    public void onSuccess(BaseEntity<ListResponseV1<List<DriverBindRecordEntity>>> data) {
                        if (!data.isSuccess()) {
                            ldList.postValue(new ListResponseV1<>(false));
                            return;
                        }
                        ldList.postValue(data.getData());
                        if (data.getData().haveMoreData()) {
                            mPage = data.getData().nextPage();
                        }
                    }

                    @Override
                    public void onFail() {
                        ldList.postValue(new ListResponseV1<>(false));
                    }
                });
    }
}
